package edu.ar.modulo3.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.ar.modulo3.model.AppConstants;
import edu.ar.modulo3.model.Usuario;
import edu.ar.modulo3.model.Vuelo;
import edu.ar.modulo3.perist.dao.IVueloDAO;
import edu.ar.modulo3.perist.factory.BaseDAOFactory;

public class ListadoEmpleadoServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	IVueloDAO vueloDAO;
	
	@Override
	public void init() throws ServletException {
		vueloDAO = BaseDAOFactory.getInstance().getVueloDAO();		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		this.process(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		this.process(req,resp);
	}
	
	private void process(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			
		HttpSession session = req.getSession();
		Usuario usuario = (Usuario) session.getAttribute(AppConstants.USERSESSION);
		
		List<Vuelo> vueloList = vueloDAO.getAll();
				
		RequestDispatcher rd = null;
		
		if ( usuario != null && vueloList != null ) {
			
			req.setAttribute(AppConstants.VUELO_LIST, vueloList);
			rd = req.getRequestDispatcher("/listadoEmpleado.jsp");						
			
		} else  {
			
			session.setAttribute(AppConstants.USERSESSION, null);
			rd = req.getRequestDispatcher("ServletError");						
		}
		
		rd.forward(req, resp);
	}

}
